package com.cz.fra.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cz.fra.entity.Comment;
import com.cz.fra.service.CommentService;
import com.cz.fra.service.ReportService;
import com.cz.fra.service.ServiceException;
import com.cz.fra.service.impl.CommentServiceImpl;
import com.cz.fra.service.impl.ReportServiceImpl;

public class PostCommentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private CommentService commentService = new CommentServiceImpl();
	
	private ReportService reportService = new ReportServiceImpl();
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		 String content = request.getParameter("content");
		 int reportId = Integer.parseInt(request.getParameter("reportId"));
		 int comment_by = Integer.parseInt(request.getParameter("userid"));
		 PrintWriter out = response.getWriter();
		 
		 Comment comment = new Comment();
		 comment.setContent(content);
		 comment.setCommentBy(comment_by);
		 comment.setReportId(reportId);
		 
	     try {
	    	 if(commentService.addComment(comment).equalsIgnoreCase("success")){
	    		 reportService.incrementCommentNum(reportId);
	    		 out.write("Success");
	    	 }else{
	    		 out.write("Failed");
	    	 }
			
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
		doGet(request,response);
	}
}
